<template>
  <div>
    
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo">
        <el-form-item label="标题">
          <el-input v-model="searchInfo.title" placeholder="标题" />
        </el-form-item>
        <el-form-item label="概述">
          <el-input v-model="searchInfo.desc" placeholder="概述" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="searchInfo.content" placeholder="内容" />
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button size="small" icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <div class="gva-btn-list">
        <el-button size="small" type="primary" icon="plus" @click="addArt">新增文章</el-button>
      </div>
      <el-table
        :data="tableData"
        row-key="ID"
      >
         
        <el-table-column align="left" label="ID" min-width="50" prop="ID" />
        <el-table-column align="left" label="标题" min-width="150" prop="title" />
        <el-table-column align="left" label="描述" min-width="150" prop="desc" />
        <el-table-column align="left" label="内容" min-width="180" prop="content" />
      

        <el-table-column label="操作" min-width="250" fixed="right">
          <template #default="scope">
            <el-popover v-model="scope.row.visible"  placement="top" width="160">
              <p>确定要删除此文章吗</p>
              <div style="text-align: right; margin-top: 8px;">
                <el-button size="small" type="primary" link @click="scope.row.visible = false">取消</el-button>
                <el-button type="primary" size="small" @click="deleteArtFunc(scope.row)">确定</el-button>
              </div>
              <template #reference>
                <el-button type="primary" link icon="delete" size="small">删除</el-button>
              </template>
            </el-popover>
            <el-button type="primary" link icon="edit" size="small" @click="openEdit(scope.row)">编辑</el-button>
            
          </template>
        </el-table-column>

      </el-table>
      <div class="gva-pagination">
        <el-pagination
          :current-page="page"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="addArtDialog"
      custom-class="Art-dialog"
      title="文章"
      :show-close="false"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <div style="height:60vh;overflow:auto;padding:0 12px;">
        <el-form ref="ArtForm" :rules="rules" :model="artInfo" label-width="80px">
           
          <el-form-item label="标题" prop="title">
            <el-input v-model="artInfo.title" />
          </el-form-item>
            <el-form-item label="描述" prop="desc">
            <el-input v-model="artInfo.desc" />
          </el-form-item>
            <el-form-item label="内容" prop="content">
            <el-input v-model="artInfo.content" />
          </el-form-item>
          

        </el-form>

      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button size="small" @click="closeAddArtDialog">取 消</el-button>
          <el-button size="small" type="primary" @click="enterAddArtDialog">确 定</el-button>
        </div>
      </template>
    </el-dialog>
    <ChooseImg ref="chooseImg" :target="artInfo" :target-key="`headerImg`" />
  </div>
</template>

<script>

export default {
  name: 'Arts',
}
</script>

<script setup>

import {
  getArtsList,
  createArt,
  deleteArt
} from '@/api/arts'


import CustomPic from '@/components/customPic/index.vue'
import ChooseImg from '@/components/chooseImg/index.vue'
import WarningBar from '@/components/warningBar/warningBar.vue'
import { setArtInfo } from '@/api/arts.js'

import { nextTick, ref, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const path = ref(import.meta.env.VITE_BASE_API + '/')
// 初始化相关

const searchInfo = ref({})
const onReset = () => {
  searchInfo.value = {}
}
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])


// 条件搜索前端看此方法
const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  if (searchInfo.value.status === '') {
    searchInfo.value.status = null
  }
  getTableData()
}


// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getArtsList({ 
  page: page.value, 
  pageSize: pageSize.value,
  ...searchInfo.value,
   })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

watch(() => tableData.value, () => {
   
})

const initPage = async() => {
  getTableData()
  
  
}

initPage()


 
const chooseImg = ref(null)
const openHeaderChange = () => {
  chooseImg.value.open()
}

 
const deleteArtFunc = async(row) => {
  const res = await deleteArt({ id: row.ID })
  if (res.code === 0) {
    ElMessage.success('删除成功')
    row.visible = false
    await getTableData()
  }
}

// 弹窗相关
const artInfo = ref({
  title: '',
  desc: '',
  content: '',
 
})

const rules = ref({
  title: [
    { required: true, message: '请输入文章名', trigger: 'blur' },
    { min: 5, message: '最低5位字符', trigger: 'blur' }
  ],
  
  desc: [
    { required: true, message: '请输入文章描述', trigger: 'blur' }
  ],
   content: [
    { required: true, message: '请输入文章内容', trigger: 'blur' }
  ],
  
})
const ArtForm = ref(null)
const enterAddArtDialog = async() => {
 
  ArtForm.value.validate(async valid => {
    if (valid) {
      const req = {
        ...artInfo.value
      }
      if (dialogFlag.value === 'add') {
        const res = await createArt(req)
        if (res.code === 0) {
          ElMessage({ type: 'success', message: '创建成功' })
          await getTableData()
          closeAddArtDialog()
        }
      }
      if (dialogFlag.value === 'edit') {
        const res = await setArtInfo(req)
        if (res.code === 0) {
          ElMessage({ type: 'success', message: '编辑成功' })
          await getTableData()
          closeAddArtDialog()
        }
      }
    }
  })
}

const addArtDialog = ref(false)
const closeAddArtDialog = () => {
  ArtForm.value.resetFields()
  artInfo.value.headerImg = ''
  
  addArtDialog.value = false
}

const dialogFlag = ref('add')

const addArt = () => {
  dialogFlag.value = 'add'
  addArtDialog.value = true
}

 


const openEdit = (row) => {
  dialogFlag.value = 'edit'
  artInfo.value = JSON.parse(JSON.stringify(row))
  addArtDialog.value = true
}

const switchEnable = async(row) => {
  artInfo.value = JSON.parse(JSON.stringify(row))
  
   
}

</script>

<style lang="scss">
.Art-dialog {
  .header-img-box {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  border-radius: 20px;
  text-align: center;
  line-height: 200px;
  cursor: pointer;
}
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9 !important;
    border-radius: 6px;
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
.nickName{
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.pointer{
  cursor: pointer;
  font-size: 16px;
  margin-left: 2px;
}
</style>
